package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.ZxYusuan;
import cn.stylefeng.guns.sys.modular.system.entity.ZxYusuanTemp;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxYusuanMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxYusuanTempMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxYusuanTempParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxYusuanTempResult;
import cn.stylefeng.guns.sys.modular.system.service.ZxYusuanTempService;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-12-07
 */
@Service
public class ZxYusuanTempServiceImpl extends ServiceImpl<ZxYusuanTempMapper, ZxYusuanTemp> implements ZxYusuanTempService {

    @Resource
    private ZxYusuanMapper zxYusuanMapper;

    @Override
    public ResponseData add(ZxYusuanTempParam param) {
        QueryWrapper<ZxYusuanTemp> wrapper = new QueryWrapper<>();
        wrapper = wrapper.eq("xuhao", param.getXuhao());
        List<ZxYusuanTemp> zxYusuanTemps = this.baseMapper.selectList(wrapper);

        if (CollectionUtil.isNotEmpty(zxYusuanTemps)) {
            return new ErrorResponseData("添加失败，序号已被使用，请填写正确序号");
        }

        ZxYusuanTemp entity = getEntity(param);
        //创建者信息
        entity.setCreateUser(LoginContextHolder.getContext().getUserId());
        entity.setCreateUserName(LoginContextHolder.getContext().getUser().getName());
        this.save(entity);

        return ResponseData.success();
    }

    @Override
    public ResponseData delete(ZxYusuanTempParam param) {
        //预算已经使用的不能被删除
        QueryWrapper<ZxYusuan> wrapper = new QueryWrapper<>();
        wrapper = wrapper.eq("xuhao", param.getXuhao());
        List<ZxYusuan> zxYusuans = zxYusuanMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(zxYusuans)) {
            return new ErrorResponseData("删除失败，该数据已在装修预算中使用。");
        }

        this.removeById(getKey(param));
        return ResponseData.success();
    }

    @Override
    public ResponseData update(ZxYusuanTempParam param) {
        ZxYusuanTemp oldEntity = getOldEntity(param);
        Integer oldXuHao=oldEntity.getXuhao();

        ZxYusuanTemp newEntity = getEntity(param);
        Integer newXuHao=newEntity.getXuhao();

        //改变序号时
        if(!oldXuHao.equals(newXuHao)){
            //修改后的序号不能是已经使用的
            QueryWrapper<ZxYusuanTemp> wrapper = new QueryWrapper<>();
            wrapper = wrapper.eq("xuhao", newXuHao);
            List<ZxYusuanTemp> zxYusuanTemps = this.baseMapper.selectList(wrapper);

            if (CollectionUtil.isNotEmpty(zxYusuanTemps)) {
                return new ErrorResponseData("修改失败，序号"+newEntity.getXuhao()+"已被使用，请填写正确序号");
            }

            //旧序号不能是预算中使用的
            QueryWrapper<ZxYusuan> wrapper2 = new QueryWrapper<>();
            wrapper2 = wrapper2.eq("xuhao", oldXuHao);
            List<ZxYusuan> zxYusuans = zxYusuanMapper.selectList(wrapper2);
            if (CollectionUtil.isNotEmpty(zxYusuans)) {
                return new ErrorResponseData("修改失败，该数据已在装修预算中使用。");
            }
        }

        ToolUtil.copyProperties(newEntity, oldEntity);
        //更新者信息
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        newEntity.setUpdateUserName(LoginContextHolder.getContext().getUser().getName());
        this.updateById(newEntity);

        return ResponseData.success();
    }

    @Override
    public ZxYusuanTempResult findBySpec(ZxYusuanTempParam param) {
        return null;
    }

    @Override
    public List<ZxYusuanTempResult> findListBySpec(ZxYusuanTempParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ZxYusuanTempParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ZxYusuanTempParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ZxYusuanTemp getOldEntity(ZxYusuanTempParam param) {
        return this.getById(getKey(param));
    }

    private ZxYusuanTemp getEntity(ZxYusuanTempParam param) {
        ZxYusuanTemp entity = new ZxYusuanTemp();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
